package kr.hyunist.androidcomplete02saeildamil.ch20;

import kr.hyunist.androidcomplete02saeildamil.*;
import android.annotation.SuppressLint;
import android.app.*;
import android.content.*;
import android.content.res.*;
import android.os.*;
import android.view.*;
import android.webkit.*;
import android.widget.*;

public class MultiPaneWidthTest2 extends Activity {
	final static String[] Site = { "http://www.google.co.kr",
			"http://www.naver.com", "http://www.daum.net", "http://zum.com" };

	final static String[] mSite = { "http://www.google.co.kr",
		"http://www.naver.com", "http://www.daum.net", "http://zum.com" };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multipanewidthtest2);
	}

	public static class SiteListFragment extends ListFragment {
		boolean mMultiPane;
		int mLastIndex = 0;

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);

			setListAdapter(new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_list_item_1, Site));
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

			if(savedInstanceState != null){
				mLastIndex = savedInstanceState.getInt("lastindex");
			}
			
			View descPanel = getActivity().findViewById(R.id.multipanewidthfl2);
			if(descPanel != null && descPanel.getVisibility() == View.VISIBLE){
				mMultiPane = true;
				onListItemClick(getListView(), null, mLastIndex, 0);
			}	
		}
		
		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			outState.putInt("lastindex", mLastIndex);
		}

		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			mLastIndex = position;
			getListView().setItemChecked(position, true);
			
			if(mMultiPane){
				FragmentManager fm = getFragmentManager();
				SiteFragment df = (SiteFragment) fm.findFragmentById(R.id.multipanewidthfl2);
				if(df == null || df.mNowIndex != position){
					df = SiteFragment.newInstance(position);
					FragmentTransaction tr = fm.beginTransaction();
					tr.replace(R.id.multipanewidthfl2, df);
					tr.commit();
				}
			} else {
				Intent intent = new Intent(getActivity(), SiteActivity.class);
				intent.putExtra("index", position);
				startActivity(intent);
			}
		}

	}

	public static class SiteActivity extends Activity {
				
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
				finish();
				return;
			}
			
			int index = getIntent().getExtras().getInt("index");
			SiteFragment details = SiteFragment.newInstance(index);
			getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
		}
	}

	public static class SiteFragment extends Fragment {
		int mNowIndex;
		WebView mWeb;
		
		public static SiteFragment newInstance(int index){
			SiteFragment df = new SiteFragment();
			df.mNowIndex = index;
			return df;
		}
		
		@SuppressLint("SetJavaScriptEnabled")
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View root = inflater.inflate(R.layout.mutipanewidthwebview, container,
					false);

			mWeb = (WebView) root.findViewById(R.id.multipanewidthwv);
			mWeb.setWebViewClient(new WebClient());
			WebSettings set = mWeb.getSettings();
			set.setJavaScriptEnabled(true);
			set.setBuiltInZoomControls(true);
			mWeb.loadUrl(mSite[mNowIndex]);
						
			return root;
		}
		
		class WebClient extends WebViewClient {

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
			
		}
	}
}
